const app = getApp() // 引入app
// 根据路由显示不同的加载文字
let degree = 0

const setTitle = (url) => {
    let title = '加载中'
    switch (url) {
        case '/user/login':
            title = '登陆中'
            break;
        case '/user/loginWXAdds':
            title = '登陆中'
            break;
        default:
            break;
    }
    return title
}

// 防止重复弹出Modal
const onLogin = (e) => {
    if (e == 1) {
        wx.showModal({
            title: '您还未登录',
            content: '请先登录再进行操作',
            success(res) {
                if (res.confirm) {
                    wx.reLaunch({
                        url: '/firstpages/authorization/index',
                    })
                    // wx.reLaunch({
                    //     url: '/pages/login/index',
                    // })
                    degree = 0
                } else if (res.cancel) {
                    degree = 0
                }
            }
        })
    } else if (e == 2) {
        // 完善资料
        wx.showModal({
            title: '您还未进行完善资料',
            content: '为了完整的进行体验，请先完成完善资料再进行操作。',
            cancelText: '暂不完善',
            confirmText: '立即完善',
            success(res) {
                if (res.confirm) {
                    wx.reLaunch({
                        url: '/firstpages/information/index',
                    })
                    // wx.reLaunch({
                    //     url: '/pages/login/index',
                    // })
                    degree = 0
                } else if (res.cancel) {
                    degree = 0
                }
            }
        })
    } 
    // else if (e == 3) {
    //     // 认证
    //     wx.showModal({
    //         title: '您还未进行认证',
    //         content: '为了完整的进行体验，请先完成认证再进行操作。',
    //         cancelText: '暂不认证',
    //         confirmText: '立即认证',
    //         success(res) {
    //             if (res.confirm) {
    //                 wx.reLaunch({
    //                     url: '/firstpages/information/index',
    //                 })
    //                 // wx.reLaunch({
    //                 //     url: '/pages/login/index',
    //                 // })
    //                 degree = 0
    //             } else if (res.cancel) {
    //                 degree = 0
    //             }
    //         }
    //     })
    // }

}

// 信息认证
const onAuth = () => {
    wx.showModal({
        title: '您还未进行信息认证',
        content: '为了完整的进行体验，请先完成信息认证再进行操作。',
        cancelText: '暂不认证',
        confirmText: '立即认证',
        success(res) {
            if (res.confirm) {
                wx.navigateTo({
                    url: '/pages/auth/index',
                })
                degree = 0
            } else if (res.cancel) {
                degree = 0
            }
        }
    })
}

const retard = (e) => {
    onLogin(e)
        ++degree
}

const request = (url, options) => {
    return new Promise((resolve, reject) => {
        // 监听网络状态，失败则跳转网络失败页面
        wx.onNetworkStatusChange(function (res) {
            if (res.isConnected == false && res.networkType == "none") {
                wx.navigateTo({
                    url: '/pages/networkanomaly/index',
                })
            }
        })
        // 加载状态文字
        wx.showLoading({
            title: setTitle(url),
        })
        var requesturl = app.globalData.baseurl;
        wx.request({
            url: `${requesturl}${url}`,
            method: options.method,
            data: options.data,
            header: {
                'content-type': options.isObj ? 'application/json' : 'application/x-www-form-urlencoded',
                'token': wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
            },
            success(request) {
                resolve(request)
                if (request.data.code !== 200) {
                    if ((request.data.code === 300 && degree == 0) && !wx.getStorageSync('userId')) {
                        wx.showToast({
                            title: request.data.message,
                            icon: 'none',
                            duration: 2000
                        })
                        retard(1)
                    } else if ((!wx.getStorageSync('storeId') && degree == 0) && wx.getStorageSync('userId')) {
                        retard(2)
                    } 
                    // else if((wx.getStorageSync('authtype') == 3 && wx.getStorageSync('userId')) && (wx.getStorageSync('storeId') && degree == 0)){
                    //     retard(3)
                    // }
                } else {
                    // wx.showToast({
                    //     title: request.data.message,
                    //     icon: 'none',   
                    //     duration: 2000   
                    // })
                }
                wx.hideLoading();
            },
            fail(error) {
                console.log(error, 'error');
                reject(error.data)
                wx.hideLoading();
            },
            // complete: () => {
            //     setTimeout(() => {
            //         wx.hideLoading();
            //     }, 100);
            // }
        })
    })
}

const get = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    } else {
        url = url
    }
    if (!options) {
        options = {}
    }
    return request(url, {
        method: 'GET',
        data: options
    })
}

//post对象
const postObj = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }
    return request(url, {
        method: 'POST',
        data: options,
        isObj: options.isObj ? false : true
    })
}

//post参数
const post = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }
    return request(url, {
        method: 'POST',
        data: options,
        isObj: false
    })
}

const put = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }

    return request(url, {
        method: 'PUT',
        data: options
    })
}

// 不能声明DELETE（关键字）
const remove = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }
    return request(url, {
        method: 'DELETE',
        data: options
    })
}
module.exports = {
    get,
    post,
    put,
    remove,
    postObj
}